package com.jingbian.cppccbackend.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池类
 */
@Configuration
public class TheadPoolConfig {
  @Bean(name = "TaskExecutor")
  public TaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    //设置核心线程数
    executor.setCorePoolSize(10);
    //设置最大线程数
    executor.setMaxPoolSize(20);
    //缓冲队列200：用来缓冲执行任务的队列
    executor.setQueueCapacity(200);
    //线程活路时间 60 秒
    executor.setKeepAliveSeconds(60);
    //线程池名的前缀：设置好了之后可以方便我们定位处理任务所在的线程池
    executor.setThreadNamePrefix("demo-thread-");
    //设置拒绝策略
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.setWaitForTasksToCompleteOnShutdown(true);
    return executor;
  }
}
